package cn.ujn.edu.sun_shine.controller;

import cn.ujn.edu.sun_shine.entity.BaseDict;
import cn.ujn.edu.sun_shine.entity.Customer;
import cn.ujn.edu.sun_shine.service.BaseDictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import org.springframework.ui.Model;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 徐冠宇 DoDo
 * @since 2020-11-06
 */
@Controller
@RequestMapping("/base-dict")
public class BaseDictController {
    @Autowired
    private BaseDictService baseDictService;
    //根据id获取对象
    @ResponseBody
    @RequestMapping("/getDictById")
    public BaseDict getDictById(@RequestParam("id") String id) {
        return baseDictService.getById(id);
    }
    @RequestMapping("/index")
    public String dict(){
        return "dictionary";

    }
    //查询列表
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(defaultValue = "1", value = "") Integer currentPage,
                       @RequestParam(value = "dictTypeCode", required = false, defaultValue = "") String dictTypeCode,
                       @RequestParam(value = "dictTypeName", required = false, defaultValue = "") String dictTypeName,
                       @RequestParam(value = "dictItemName", required = false, defaultValue = "") String dictItemName,
                       @RequestParam(value = "dictSort", required = false, defaultValue = "") String dictSort) {

        //传递给前端分页url

        model.addAttribute("dictTypeCode",dictTypeCode);
        model.addAttribute("dictTypeName",dictTypeName);
        model.addAttribute("dictItemName",dictItemName);
        model.addAttribute("dictSort",dictSort);
//        //启动构造器
        QueryWrapper<BaseDict> qw = new QueryWrapper<>();
        if (!dictTypeCode.isEmpty()) {
            qw.eq("dict_type_code", dictTypeCode);
        }
        if (!dictTypeName.isEmpty()) {
            qw.eq("dict_type_name", dictTypeName);
        }
        if (!dictTypeName.isEmpty()) {
            qw.eq("dict_type_name", dictTypeName);
        }
        if (!dictSort.isEmpty()) {
            qw.eq("dict_sort", dictSort);
        }
        Page<BaseDict> page = new Page<>(currentPage, 5);

        Long total = baseDictService.page(page, qw).getPages();
        if (total - currentPage >= 8) {

            model.addAttribute("start", currentPage);
            model.addAttribute("current", currentPage);
            model.addAttribute("total", currentPage + 8);


        } else {
            if(total>8)
                model.addAttribute("start", total - 8);
            else model.addAttribute("start", 1);
            model.addAttribute("current", currentPage);
            model.addAttribute("total", total);
        }
        model.addAttribute("final", total);
        //上一页
        Integer pre;
        if (currentPage > 1)
            pre = currentPage - 1;
        else pre = 1;
        model.addAttribute("pre", pre);
        //下一页
        Integer next;
        if (currentPage < total)
            next = currentPage + 1;
        else next = Math.toIntExact(total);
        model.addAttribute("next", next);
        //查询列表
        List<BaseDict> baseDicts = baseDictService.page(page, qw).getRecords();

        model.addAttribute("baseDicts", baseDicts);


        return "dictionary";
    }
    @RequestMapping("/create")
    @ResponseBody
    public String create(BaseDict baseDict) {
        baseDict.setDictId("40");
        boolean b = baseDictService.save(baseDict);
        if (b) {
            return "OK";
        }
        return null;
    }
    @RequestMapping("/update")
    @ResponseBody
    public String update(BaseDict baseDict) {

        boolean b = baseDictService.saveOrUpdate(baseDict);
        if (b) {
            return "OK";
        }
        return null;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id) {
        boolean b = baseDictService.removeById(id);
        if (b) {
            return "OK";
        }
        return null;
    }
}
